<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2018 http://www.donglixia.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: 十万马 <962863675@qq.com>
// +----------------------------------------------------------------------
// | DateTime: 2018-02-09 16:17
// +----------------------------------------------------------------------

namespace app\index\model;

use app\common\model\Goods as CoreGoods;
use think\Db;
use think\Request;

class Goods extends CoreGoods
{
    /**
     * 默认需要获取的字段
     * @var string
     */
    private $field_goods = 'goods_id,cat_id,goods_name,short_name,price,years,years as years_text,
        area,img_url,img_url_thumb,read_num,praise_num,level,country,country as country_text';

    /**
     * 默认需要获取的字段
     * @var string
     */
    private $language = 1;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        if ($this->request->module() == 'english') {
            $this->language = 2;
        } elseif ($this->request->module() == 'tradi') {
            $this->language = 3;
        }
    }
    /**
     * 获取产品数据列表
     * @param array $map
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getDataList($map = [])
    {
        $param = $this->request->param();
        $limit = !empty($param['limit']) ? $param['limit'] : 15 ;
        $where = [];
        if (!empty($param['cat_id'])) {
            $cat_ids = get_child_ids($param['cat_id'], 'goods_cat');//获取子分类cat_id
            $map['cat_id'] = ['in', $cat_ids];//包含所有子系列

            $goods_ids = Db::name('goods_ext_cat')->whereIn('cat_id', $cat_ids)->column('goods_id');
            $where['goods_id'] = ['in', $goods_ids];//扩展分类
        }

        if (!empty($param['country'])) {
            $map['country'] = $param['country'];//国家
        }
        if (!empty($param['years'])) {
            $map['years'] = $param['years'];//年份
        }

        $map['language'] = $this->language;//语言
        $map['state'] = 1;//已发布状态
        //不包含“首彩周边”
        $cat_id_not = (isset($param['cat_id']) && !in_array($param['cat_id'], [71,72,73])) ? [71,72,73] : 0;
        return $this->where($map)->whereNotIn('cat_id', $cat_id_not)->whereOr($where)
            ->field($this->field_goods)
            ->order('sort_num asc, goods_id desc')
            ->paginate($limit, false, ['query'=>$param]);
    }

    /**
     * 获取分类数据列表
     * @param array $map
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function getLocationTree($map = [])
    {
        $param = $this->request->param();

        if (!empty($param['cat_id'])) {
            $curr_cat_id = $param['cat_id'];
        } else {
            if ($this->request->module() == 'english') {
                $curr_cat_id = 25;
            } elseif ($this->request->module() == 'tradi') {
                $curr_cat_id = 48;
            } else {
                $curr_cat_id = 1;
            }
        }

        $map['language'] = $this->language;//语言
        $map['state'] = 1;//状态：启用
        $list = Db::name('goods_cat')->where($map)->field('cat_id,pid,cat_name')->order('sort_num asc, cat_id desc')->select();
        $list = $this->currCatDisplay($list, $curr_cat_id);
        return \Tree::getTree($list, 'cat_id', 'pid');
    }

    /**
     * 标记当前分类及父级分类为显示状态
     * @param $list
     * @param int $cat_id
     * @return mixed
     */
    protected function currCatDisplay($list, $cat_id = 1)
    {
        $curr_cat_ids = get_parent_ids($cat_id, 'goods_cat');//当前分类父级id集合
        foreach ($list as $k=>$v) {
            $list[$k]['is_display'] = in_array($v['cat_id'], $curr_cat_ids) ? 1 : 0;
        }
        return $list;
    }

    /**
     * 获取产品详情
     * @param array $map
     * @return array|bool|false|\PDOStatement|string|\think\Model
     */
    public function getGoodsInfo($map = [])
    {
        if (empty($map)) {
            return false;
        }
        $this->field_goods .= ',goods_style,food_mix,wine_label,keywords,buy_url,detail,detail_img,describe,area_img,area_detail,goods_name_style';
        $res = $this->where($map)->field($this->field_goods)->find();
        if (empty($res)) {
            return false;
        }
        return $res;
    }

    /**
     * 获取某一位置banner
     * @param $cat_code
     * @param $basic_code
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getBanner($cat_code = 'banner', $basic_code = 'AA')
    {
        $basic_pid = Db::name('basic_info')->where('cat_code', $cat_code)->where('basic_code', $basic_code)->value('basic_id');
        return Db::name('basic_banner')
            ->where(['location'=>$basic_pid, 'language'=>$this->language, 'state'=>1])
            ->field('banner_id,img_name,img_name_style,img_url,link,describe')
            ->find();
    }
}